//
//  VCSelectSeat.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/2/13.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class VCSelectSeat: VCBase {
    
    // 订单信息
    public var orderInfo:OrderInfo?
    // 乘机人信息
    public var passenger:PassengerInfo?
    
    // 顶部布局
    private lazy var selectSeatTopView:SelectSeatTopView = {
        let uiY = realHeight + MARGIN10
        let ui = SelectSeatTopView(frame: CGRect(x: 0, y: uiY, width: SCREEN_WIDTH, height: MARGIN80))
        ui.backgroundColor = UIColor.white
        return ui
    }()
    
    // 躯干布局
    private lazy var selectSeatBodyView:SelectSeatBodyView = {
        let ui = SelectSeatBodyView()
        ui.backgroundColor = UIColor.white
        let uiY = selectSeatTopView.frame.maxY + MARGIN10
        ui.frame = CGRect(x: 0, y: uiY, width: SCREEN_WIDTH, height: SCREEN_HEIGHT - uiY - realHeight)
        return ui
    }()
    
    // 确认座位按钮
    private lazy var selecSeatSubmitView: SelecSeatSubmitView = {
        let ui = SelecSeatSubmitView()
        ui.backgroundColor = COLOR_LIGHT_WHITE2
        ui.frame = CGRect(x: 0, y: SCREEN_HEIGHT - realHeight, width: SCREEN_WIDTH, height: realHeight)
        return ui
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        init_view()
        init_data()
        init_event()
    }
}

// MARK: 逻辑部分
extension VCSelectSeat {
    
    private func saveSeat(seat:String,seatIndex:Int) -> Void {
        guard let order = orderInfo else {
            return
        }
        // 拼接数据
        let request = NSMutableDictionary()
        request.setValue(seat, forKey: "seat")
        request.setValue(order.city, forKey: "city")
        request.setValue(order.orderid, forKey: "orderid")
        request.setValue(order.endcity, forKey: "endcity")
        request.setValue(order.departdate, forKey: "departdate")
        request.setValue(order.flightnumberid, forKey: "flightnumberid")
        request.setValue(String(describing: seatIndex), forKey: "seatindexs")
        // 请求
        self.doPost(api: "save/order/seats", dict: request) { (dict) in
            if self.isServerSuccess(dict: dict) != 1200 {
                self.show(text: "预定失败")
                return
            }
            self.show(text: "预定成功")
            self.navigationController?.popViewController(animated: true)
        }
    }
}

// MARK: 初始化
extension VCSelectSeat {
    
    private func init_event() -> Void {
        
        // 座位号被点击了
        selectSeatBodyView.complete = {seat,seatIndex in
            // 修改显示的文本
            self.selecSeatSubmitView.modifyShowSeat = seat
            // 确认按钮被点击了
            self.selecSeatSubmitView.complete = {
                self.saveSeat(seat: seat, seatIndex: seatIndex)
            }
        }
    }
    
    private func init_data() -> Void {
        guard let order = orderInfo else {
            return
        }
        // 拼接数据
        let request = NSMutableDictionary()
        request.setValue(order.city, forKey: "city")
        request.setValue(order.orderid, forKey: "orderid")
        request.setValue(order.endcity, forKey: "endcity")
        request.setValue(order.departdate, forKey: "departdate")
        request.setValue(order.flightnumberid, forKey: "flightnumberid")
        // 请求
        self.doPost(api: "search/order/seats", dict: request) { (dict) in
            if self.isServerSuccess(dict: dict) != 1200 {
                self.selectSeatBodyView.orderInfo = order
                return
            }
            // 解析数据
            guard let arrs = dict.value(forKey: SP_BODYREQUEST) as? [String] else {
                return
            }
            // 是否已选座位号
            if order.seatnumber != nil {
                self.selecSeatSubmitView.modifyShowSeatBtn = "您已选座位号：\(order.seatnumber ?? "")"
                 self.selectSeatBodyView.imgIsUserInteractionEnabled = false
            }
            // 更新数据
            self.selectSeatBodyView.selectedIndex = arrs
            self.selectSeatBodyView.orderInfo = order
            
        }
    }
    
    private func init_view() -> Void {
        view.addSubview(backroundWhite)
        view.addSubview(selectSeatTopView)
        view.addSubview(selectSeatBodyView)
        view.addSubview(selecSeatSubmitView)
    }
}
